<?php
function lilina_userapi_update_story($args)
{
    // Get arguments from argument array
    extract($args);

    // Argument check - make sure that all required arguments are present,
    // if not then set an appropriate error message and return

    $invalid = array();
    if (!isset($title) || !is_string($title)) {
        $invalid[] = 'title';
    } 
    if (!isset($link) || !is_string($link)) {
        $invalid[] = 'link';
    } 
    if (count($invalid) > 0) {
        $msg = xarML('Invalid #(1) for #(2) function #(3)() in module #(4)', join(', ', $invalid), 'user', 'update_story', 'lilina');
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    } 
    // Get Category Info Now
    if (isset($linkid)) {
        $feed = xarModAPIFunc('lilina',
                              'user',
                              'get',
                               array('id'    => $linkid));
    }
    //var_dump($feed); return;

    // First check to see if unique
    $check_unique = xarModAPIFunc('lilina',
                                  'user',
                                  'check_unique',
                                  array('unique'    => $unique));

    // We get back a unique ID here, so this story has already made it in the DB.
    if ($check_unique){
      return true;
    }



    // Security Check
    if(!xarSecurityCheck('OverviewLilina')) return;

    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $stable = $xartable['story'];

    // Get next ID in table
    $nextId = $dbconn->GenId($stable);

    // Add item
    $query = "INSERT INTO $stable (
              xar_id,
              xar_title,
              xar_summary,
              xar_link,
              xar_timestamp,
              xar_ctitle,
              xar_curl,
              xar_cfavicon,
              xar_keyword,
              xar_unique)
            VALUES (
              ?, 
              ?,
              ?,
              ?,
              ?,
              ?,
              ?,
              ?,
              ?,
              ?)";
    $result =& $dbconn->Execute($query, array($nextId, $title, $summary, $link, $timestamp, $ctitle, $curl, $cfavicon, $keyword, $unique));
    if (!$result) return;
    // Get the ID of the item that we inserted
    $id = $dbconn->PO_Insert_ID($stable, 'xar_id');
    // Create Category Linkage
    if (xarModIsHooked('categories','lilina')) {
        xarModAPIFunc('categories', 'admin', 'linkcat',
                       array('cids'  => array_unique($feed['cids']),
                             'iids'  => array($id),
                             'itemtype' => 1,
                             'modid' => xarModGetIDFromName('lilina')));
    }
    $args['module'] = 'lilina';
    $args['itemtype'] = 1;
    $args['itemid'] =$id;
    $args['keywords'] = $keyword;
    xarModCallHooks('item', 'create', $id, $args);
    return $id;
} 
?>